package com.xnyzc.lhy.resource.controller.cp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.resource.service.cp.BankCardAuditService;
import com.xnyzc.lhy.resource.util.HttpUtils;
import com.xnyzc.lhy.resource.util.cp.BankUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-09-18
 */
@RestController
@RequestMapping("check")
public class LgBankCardAuditController {

    @Autowired
    private BankCardAuditService bankCardAuditService;

    /**
     * 银行卡效验 测试卡号 6225380004804588
     * @param cardNo 银行卡号
     * @return Map
     */
    @ApiOperation(value = "银行卡效验", notes = "银行卡效验")
    @GetMapping("/bankCardCheck")
    public Rv bankCardCheck(@RequestParam("cardNo")String cardNo) {

        String host = "https://ccdcapi.alipay.com";
        String path = "/validateAndCacheCardInfo.json";
        String method = "GET";

        Map<String, String> headers = new HashMap<String, String>(1);
        Map<String, String> params = new HashMap<String, String>(2);
        params.put("_input_charset", "utf-8");
        params.put("cardNo", cardNo);
        params.put("cardBinCheck", "true");

        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, params);
            String res = EntityUtils.toString(response.getEntity());
            JSONObject resObj = JSON.parseObject(res);

            boolean status = resObj.getBoolean("validated");
            if (status) {
                String cardType = resObj.getString("cardType");
                String bankType = resObj.getString("bank");
                String bankCardNumber = resObj.getString("key");

                Map<String, String> map = new HashMap<>(3);
                map.put("cardType", BankUtil.getCardText(cardType));
                map.put("card", cardType);
                map.put("bankType", BankUtil.getBankText(bankType));
                map.put("bank", bankType);
                map.put("bankCardNumber", bankCardNumber);

                return Rv.wrap(map);
            } else {
                return Rv.wrap(EErrorCode.BANK_CARD_CHECK_FILED, resObj.getString("messages"));
            }
        } catch (Exception e) {
            throw PangException.create(EErrorCode.c403);
        }

    }

    /**
     * 银行卡三要素鉴权
     * @param cardNo 银行卡号
     * @param idCard 身份证号
     * @param name 姓名
     */
    @ApiOperation(value = "银行卡鉴权", notes = "银行卡鉴权")
    @GetMapping("/bankCardAuthentication")
    public Rv bankCardAuthentication(
            @RequestParam("driverId")Long driverId,
            @RequestParam("cardNo")String cardNo,
            @RequestParam("idCard")String idCard,
            @RequestParam("name")String name,
            @RequestParam("tag")String tag
    ) {
        return bankCardAuditService.bankCardAuthentication(cardNo, idCard, name, driverId, tag);
    }

}
